import { parse } from 'url';
import webpath from '@/utils/webpath';
// mock tableListDataSource
let tableListDataSource = [];
for (let i = 0; i < 46; i += 1) {
    tableListDataSource.push({
        res_id: `李海波 res_id ${i}`,
        auth_id: `李海波 auth_id ${i}`,
        status: Math.floor(Math.random() * 10) % 4,
    });
}
let userListDataSource = [];
for (let i = 0; i < 46; i += 1) {
    userListDataSource.push({
       	res_id: `李海波 res_id ${i}`,
        auth_id: `李海波 auth_id ${i}`,
        status: Math.floor(Math.random() * 10) % 4,
    });
}

let tableMenu = [];
	tableMenu.push(
	  { title: '黄金', key: '1' ,id: 2,href: "htttp://www.baidu",leaf: false,text:'这是更新表单',indexs: 12,name: '姜旭聪'},
      { 
      	title: '矿石', 
      	key:'00000000',
      	children: [
      		{ title: '铜矿', key: '4'},
      		{ title: '银矿', key: '5'},
      		{ title: '煤矿', key: '6', isLeaf: true },
      	] 
      },
      { title: '塑料', key: '3', isLeaf: true },
	);

let treeNodeArray = [
'0','1','2',
];
let treeNode = 2;

function getAllResTreeByAuthId (req,res,u,b) {
	treeNode += 1;
	treeNodeArray = [...treeNodeArray,treeNode];
	let result = {
        list: treeNodeArray,
    };
	return res.json(result);
}

function getMenuTree(req, res, u) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const params = parse(url, true).query;

    let dataSource = tableMenu;
    const result = {
        list: dataSource,
    };
    return res.json(result);
}
function getProResourceTreeByLevelCode (req,res,u,b){
	return getMenuTree(req, res, u)
}



function getProAuth(req, res, u) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const params = parse(url, true).query;

    let dataSource = userListDataSource;
    if (params.sorter!=undefined) {
        const s = params.sorter.split('_');
        dataSource = dataSource.sort((prev, next) => {
            if (s[1] === 'descend') {
            return next[s[0]] - prev[s[0]];
        }
        return prev[s[0]] - next[s[0]];
    });
    }

    if (params.status!=undefined) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
    filterDataSource = filterDataSource.concat(
    dataSource.filter(data =>
    {

        if(data.status!=undefined){
        return parseInt(data.status, 10) === parseInt(s[0], 10);
        }
    })
    );
    });
    dataSource = filterDataSource;
    }

    if (params.name!=undefined) {
    dataSource = dataSource.filter(data => {
        if(data.name!=undefined){
        return data.name.indexOf(params.name) > -1;
        }

    });
    }

    let pageSize = 10;
    if (params.pageSize!=undefined) {
        pageSize = params.pageSize * 1;
    }

    const result = {
        list: dataSource,
        pagination: {
            total: dataSource.length,
            pageSize,
            current: parseInt(params.currentPage, 10) || 1,
        },
    };

    return res.json(result);
}
function getAllForProAuth(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



    return getProAuth(req, res, u);
}

function getProResAuth(req, res, u) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const params = parse(url, true).query;

    let dataSource = tableListDataSource;

    if (params.sorter!=undefined) {
        const s = params.sorter.split('_');
        dataSource = dataSource.sort((prev, next) => {
            if (s[1] === 'descend') {
            return next[s[0]] - prev[s[0]];
        }
        return prev[s[0]] - next[s[0]];
    });
    }

    if (params.status!=undefined) {
    const status = params.status.split(',');
    let filterDataSource = [];
    status.forEach(s => {
    filterDataSource = filterDataSource.concat(
    dataSource.filter(data =>
    {

        if(data.status!=undefined){
        return parseInt(data.status, 10) === parseInt(s[0], 10);
        }
    })
    );
    });
    dataSource = filterDataSource;
    }

    if (params.name!=undefined) {
    dataSource = dataSource.filter(data => {

        if(data.name!=undefined){
        return data.name.indexOf(params.name) > -1;
        }

    });
    }

    let pageSize = 10;
    if (params.pageSize!=undefined) {
        pageSize = params.pageSize * 1;
    }

    const result = {
        list: dataSource,
        pagination: {
            total: dataSource.length,
            pageSize,
            current: parseInt(params.currentPage, 10) || 1,
        },
    };

    return res.json(result);
}



function deleteByPrimaryKeyForProResAuth(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;

    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



    return getProResAuth(req, res, u);
}


function selectByPrimaryKeyForProResAuth(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProResAuth(req, res, u);
}



function insertForProResAuth(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;



    const i = Math.ceil(Math.random() * 10000);
    tableListDataSource.unshift({
        res_id: `李海波 res_id ${i}`,
        auth_id: `李海波 auth_id ${i}`,
        status: Math.floor(Math.random() * 10) % 4,
    });


    return getProResAuth(req, res, u);
}




function updateForProResAuth(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;

    if(id!=undefined)
    tableListDataSource = tableListDataSource.map(item => {
        if (item.id === id) {
        Object.assign(item, { desc, name });
        return item;
    }
    return item;
});


    return getProResAuth(req, res, u);
}


function deleteByMapForProResAuth(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProResAuth(req, res, u);
}



function updateByMapForProResAuth(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProResAuth(req, res, u);
}

function getOneForProResAuth(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(undefined!=id)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



    return getProResAuth(req, res, u);
}

function getAllForProResAuth(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



    return getProResAuth(req, res, u);
}

function queryPageForProResAuth(req, res, u, b) {


    return getProResAuth(req, res, u);
}
function getAllProResourceTreeByAuthID(req, res, u, b) {
    return getProResAuth(req, res, u);
}
function getPageAuthByProResourceTreeId(req, res, u, b) {
    return getProResAuth(req, res, u);
}

function countForProResAuth(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProResAuth(req, res, u);
}


function insertBatchForProResAuth(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProResAuth(req, res, u);
}



function deleteBatchForProResAuth(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProResAuth(req, res, u);
}


function updateBatchForProResAuth(req, res, u, b) {
    let url = u;
    if (!url || Object.prototype.toString.call(url) !== '[object String]') {
        url = req.url; // eslint-disable-line
    }

    const body = (b && b.body) || req.body;
    const { method, name, age, id } = body;


    if(id!=undefined)
    tableListDataSource = tableListDataSource.filter(item => id.indexOf(item.id) === -1);



return getProResAuth(req, res, u);
}




export default {
    'GET /${webpath}/web/api/service/proresauth/delete': deleteByPrimaryKeyForProResAuth,
    'GET /${webpath}/web/api/service/proresauth/get': selectByPrimaryKeyForProResAuth,
    'POST /${webpath}/web/api/service/proresauth/insert': insertForProResAuth,
    'POST /${webpath}/web/api/service/proresauth/update': updateForProResAuth,
    'POST /${webpath}/web/api/service/proresauth/deleteByMap': deleteByMapForProResAuth,
    'POST /${webpath}/web/api/service/proresauth/updateByMap': updateByMapForProResAuth,
    'POST /${webpath}/web/api/service/proresauth/getOne': getOneForProResAuth,
    'POST /${webpath}/web/api/service/proresauth/getAll': getAllForProResAuth,
    'POST /${webpath}/web/api/service/proresauth/queryPage': queryPageForProResAuth,
    'POST /${webpath}/web/api/service/proresauth/count': countForProResAuth,
    'POST /${webpath}/web/api/service/proresauth/insertBatch': insertBatchForProResAuth,
    'POST /${webpath}/web/api/service/proresauth/deleteBatch': deleteBatchForProResAuth,
    'POST /${webpath}/web/api/service/proresauth/updateBatch': updateBatchForProResAuth,
    
    'POST /${webpath}/web/api/service/proresourceaction/getProResourceTreeByLevelCode': getProResourceTreeByLevelCode,
    'POST /${webpath}/web/api/service/proresauth/getAllProResourceTreeByAuthID': getAllProResourceTreeByAuthID,
    'POST /${webpath}/web/api/service/proresauth/getPageAuthByProResourceTreeId': getPageAuthByProResourceTreeId,
    
    'POST /${webpath}/web/api/service/proresourceaction/insert': insertForProResAuth,
    'POST /${webpath}/web/api/service/proresourceaction/update': updateForProResAuth,
    'POST /${webpath}/web/api/service/proresourceaction/delete': deleteByPrimaryKeyForProResAuth,
    
    'POST /${webpath}/web/api/service/proauthority/queryPage': queryPageForProResAuth,
};
